package com.goushu.service.impl;

import com.goushu.mapper.CustomerMapper;
import com.goushu.pojo.*;
import com.goushu.service.CustomerService;
import com.goushu.utils.Md5Util;
import com.goushu.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public Customer findByUserName(String username) {
        Customer c = customerMapper.findByUserName(username);
        return c;
    }


    @Override
    public void customerRegister(Customer customer) {
        String md5String = Md5Util.getMD5String(customer.getPassword());
        customer.setPassword(md5String);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        customerMapper.register(customer);
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        customerMapper.updatePwd(Md5Util.getMD5String(newPwd), id);
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        customerMapper.updateAvatar(avatarUrl, id);
    }

    @Override
    public void update(Customer customer) {
        customer.setUpdateTime(LocalDateTime.now());
        customerMapper.update(customer);
    }

    @Override
    @Transactional
    public void order(Map<String, String> data, List<Map<String, Object>> books) {
        log.info("books: {}", books);
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer cId = (Integer) map.get("id");
        Orders orders = new Orders();
        try {
            BeanUtils.populate(orders, data);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        orders.setCId(cId);
        orders.setCreateTime(LocalDateTime.now());
        log.info("order用户信息: {}", orders);
        customerMapper.order(orders);
        Integer oId = orders.getId();
        List<OrdersDetail> ordersDetails = new ArrayList<>();
        for(int i = 0; i < books.size(); i++) {
            OrdersDetail ordersDetail = new OrdersDetail();
            try {
                BeanUtils.populate(ordersDetail, books.get(i));
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            }
            ordersDetail.setOId(oId);
            ordersDetail.setState(1);
            ordersDetails.add(ordersDetail);

            Integer bId = (Integer) books.get(i).get("bid");
            Integer count = (Integer) books.get(i).get("count");
            customerMapper.saleOfBook(bId, count);
        }
        customerMapper.orderDetail(ordersDetails);
    }

    @Override
    public List<OrderQuery> listOrder() {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        return customerMapper.listOrder(id);
    }

    @Override
    public void addComment(AddComment addComment) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer cId = (Integer) map.get("id");
        addComment.setCreateTime(LocalDateTime.now());
        customerMapper.addComment(addComment, cId);
    }

    @Override
    public void modifyOrder(Integer id) {
        customerMapper.modifyOrder(id);
    }


}
